package org.jianfa.learn.springmvc.constraints;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MyConstraintValidator implements ConstraintValidator<MyConstraint, String> {

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (s.length() < 3) {
            constraintValidatorContext.disableDefaultConstraintViolation();
            ConstraintValidatorContext.ConstraintViolationBuilder builder =
                    constraintValidatorContext.buildConstraintViolationWithTemplate("使用自定义注解");
            builder.addConstraintViolation();
            return false;
        }
        return true;
    }
}
